/*
 * @Description: 角色reducer
 * @Author: Rfan
 * @Date: 2022-03-31 13:22:48
 * @LastEditTime: 2022-05-25 08:59:44
 */

import { jsonClone } from '@/utils';
import { delItemByIdArray } from '@/utils/list';
import {
  CHANGE_ACTIVE_TAB,
  CLEAR_SELECT_USER,
  DELETE_ROLE,
  DELETE_ROLE_USER,
  HIDE_ADD_DIALOG,
  INCERT_ROLE,
  SET_ROLE_LIST,
  SET_ROLE_USER_LIST,
  SET_SELECT_ROLE_ID,
  SET_SELECT_UNIT_ID,
  SET_SELECT_USER_ID,
  SHOW_ADD_DIALOG
} from '../constants';

export const RoleReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 设置选中单位ID
     * @param {Object} id 选中单位的ID
     */
    case SET_SELECT_UNIT_ID:
      return {
        ...state,
        selectUnitId: action.id
      };

    /**
     * @description: 设置选中单位ID
     * @param {string} id 选中单位的ID
     */
    case SET_SELECT_ROLE_ID:
      return {
        ...state,
        selectRoleId: action.id
      };

    /**
     * @description: 设置角色列表
     * @param {Object[]} data 角色列表
     */
    case SET_ROLE_LIST:
      return {
        ...state,
        roleList: action.data
      };

    /**
     * @description: 打开创建角色弹窗
     */
    case SHOW_ADD_DIALOG:
      return {
        ...state,
        addRoleDialog: {
          visible: true
        }
      };

    /**
     * @description: 关闭创建角色弹窗
     */
    case HIDE_ADD_DIALOG:
      return {
        ...state,
        addRoleDialog: {
          visible: false
        }
      };

    /**
     * @description: 切换tab
     */
    case CHANGE_ACTIVE_TAB:
      return {
        ...state,
        selectTab: `${action.id}`
      };

    /**
     * @description: 创建成功后将新创建的角色插入列表
     * @param {object} data 新创建的角色
     */
    case INCERT_ROLE:
      return {
        ...state,
        roleList: [...state.roleList, action.data],
        addRoleDialog: {
          visible: false
        }
      };

    /**
     * @description: 删除角色方法
     * @param {number[]} data 删除的角色ID
     */
    case DELETE_ROLE: {
      const roleList = delItemByIdArray(action.ids, state.roleList);
      return {
        ...state,
        roleList,
        selectRoleId: roleList.length > 0 ? `${roleList[0].id}` : ''
      };
    }

    // ---------------------- 角色用户列表 ------------------------------------------
    /**
     * @description: 设置角色用户列表
     * @param {number|number[]} id
     */
    case SET_ROLE_USER_LIST:
      return {
        ...state,
        userList: action.data
      };

    /**
     * @description: 设置选中用户ID
     * @param {Object} id 选中用户的ID
     */
    case SET_SELECT_USER_ID: {
      const list = jsonClone(state.selectUserIds);
      let selectUserIds = [];
      if (action.way) {
        list.push(action.id);
        selectUserIds = list;
      } else {
        for (let i = 0; i < list.length; i++) {
          if (action.id === list[i]) {
            list.splice(i, 1);
            selectUserIds = list;
          }
        }
      }
      return {
        ...state,
        selectUserIds
      };
    }

    /**
     * @description: 删除角色中用户
     * @param {number[]} ids 被删除的用户id
     * @param {number[]} srtIds 被删除的用户id
     * @return {*}
     */
    case DELETE_ROLE_USER: {
      const userList = state.userList.filter((item) => action.ids.indexOf(item.userRole.id) < 0);
      return {
        ...state,
        userList,
        selectUserIds: []
      };
    }

    /**
     * @description: 清楚选中的用户
     */
    case CLEAR_SELECT_USER:
      return {
        ...state,
        selectUserIds: []
      };

    default:
      console.log('untouched', action);
      return state;
  }
};
